package com.taijihuabao.modules.controller;

import com.github.pagehelper.PageInfo;
import com.taijihuabao.common.jsonresult.JsonResult;
import com.taijihuabao.modules.entity.MenuEntity;
import com.taijihuabao.modules.model.basedto.IdDTO;
import com.taijihuabao.modules.model.menu.dto.MenuCrFromDTO;
import com.taijihuabao.modules.model.menu.dto.MenuListFromDTO;
import com.taijihuabao.modules.model.menu.dto.MenuUpFromDTO;
import com.taijihuabao.modules.model.menu.vo.MenuInfoVO;
import com.taijihuabao.modules.service.MenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 *菜单管理
 *
 * @author Wang
 */
@Api(tags = "菜单管理")
@RestController
@RequestMapping("api/v1/menu")
public class MenuController {

    @Autowired
    MenuService menuService;

    /**
     * 菜单列表
     *
     * @param listFromDTO
     * @return
     */
    @ApiOperation("菜单列表")
    @GetMapping
    public JsonResult selectMenus(MenuListFromDTO listFromDTO) {
        PageInfo pageInfo = menuService.selectMenus(listFromDTO);
        return JsonResult.success(pageInfo);
    }

    /**
     * 获取所有菜单
     *
     * @return
     */
    @ApiOperation("所有菜单")
    @GetMapping("/selectAll")
    public JsonResult selectAll(){
        List<MenuEntity> nodeTree = menuService.selectAllMenus();
        return JsonResult.success(nodeTree);
    }

    /**
     * 菜单信息
     *
     * @param idDTO
     * @return
     */
    @ApiOperation("菜单信息")
    @GetMapping("/info")
    public JsonResult selectById(IdDTO idDTO){
        MenuInfoVO menuInfoVO = menuService.selectById(idDTO.getId());
        return JsonResult.success(menuInfoVO);
    }

    /**
     * 获取父级菜单
     *
     * @param idDTO
     * @return
     */
    @ApiOperation("获取父级菜单")
    @GetMapping("/selectParent")
    public JsonResult selectParent(IdDTO idDTO){
        List<MenuEntity> menuEntityList = menuService.selectParent(idDTO.getId());
        System.out.println(menuEntityList);
        return JsonResult.success(menuEntityList);
    }

    /**
     * 新增菜单
     *
     * @param crFromDTO
     * @return
     */
    @ApiOperation("新增菜单")
    @PostMapping("/addMenu")
    public JsonResult insertMenu(@RequestBody MenuCrFromDTO crFromDTO) {
        menuService.insertMenu(crFromDTO);
        return JsonResult.success();
    }

    /**
     * 修改菜单
     *
     * @param upFromDTO
     * @return
     */
    @ApiOperation("修改菜单")
    @PutMapping("/updateMenu")
    public JsonResult updateMenu(@RequestBody MenuUpFromDTO upFromDTO) {
        menuService.updateMenu(upFromDTO);
        return JsonResult.success();
    }

    /**
     * 删除菜单
     *
     * @param idDTO
     * @return
     */
    @ApiOperation("删除菜单")
    @DeleteMapping("/deleteMenu")
    public JsonResult deleteMenu(@RequestBody IdDTO idDTO) {
        menuService.deleteMenu(idDTO.getId());
        return JsonResult.success();
    }
}
